import Directions from "@arcgis/core/widgets/Directions";
import RouteLayer from "@arcgis/core/layers/RouteLayer";
import { map, mapView } from "@/api/map";

let directions: Directions | null = null;
let routerLayer = new RouteLayer({
  url: "https://124.222.85.168:6443/arcgis/rest/services/network_layer/netlayer728/NAServer/route",
});
export const routeAnalyse = () => {
  map.add(routerLayer);
  if (directions) {
    mapView.ui.remove(directions);
    directions.destroy();
    directions = null;
  }
  directions = new Directions({
    view: mapView,
    layer: routerLayer,
  });

  mapView.ui.add(directions, {
    position: "top-right",
  });
};
export const removeRouteAnalyse = () => {
  //移除控件
  if (directions != null) {
    mapView.ui.remove(directions);
  }
  //销毁实例
  if (directions) {
    directions.destroy();
  }
  //垃圾回收
  directions = null;
};

export { directions };
